package com.service.impl;

import com.ReturnDTO.ReturnDTO;
import com.entity.Student;
import com.mapper.StudentMapper;
import com.service.StudentService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 学生信息业务层
 */
@Service
public class StudentServiceImpl implements StudentService {

  @Autowired
  private StudentMapper studentMapper;

  /**
   * 添加学生信息，判断参数
   * @param student
   * @return
   */
  @Override
  public ReturnDTO addStudent(Student student) {
    if (student == null) {
      return ReturnDTO.getReturnDTO(500, "未接收到参数值");
    }
    int rows = studentMapper.inseterStudent(student);
    if (rows == 0) {
      return ReturnDTO.getReturnDTO(500, "添加失败");
    }
    return ReturnDTO.getReturnDTO(200, "添加成功", "/student/toStudentPage");
  }

  /**
   * 查询学生信息，判断（200，成功，500，失败）返回到前端页面
   * @param student
   * @return
   */
  @Override
  public ReturnDTO selectStudent(Student student) {
    if (student == null) {
      return ReturnDTO.getReturnDTO(500, "未接收到参数值");
    }
    int page = student.getPage() - 1;
    int number = student.getNumber();
    student.setPage(page * number);
    List<Student> selectStudent = studentMapper.selectStudent(student);
    int total = studentMapper.selectStudentTotal();
    if (ObjectUtils.isEmpty(selectStudent)) {
      return ReturnDTO.getReturnDTO(500, "查询失败");
    }
    if (total == 0){
      return ReturnDTO.getReturnDTO(500,"接收失败");
    }

    return ReturnDTO.getReturnDTO(200, "查询成功", selectStudent,total);
  }

  /**
   * 查询单条学生信息，判断返回参数
   * @param student
   * @return
   */
  @Override
  public ReturnDTO selectAloneStudent(Student student) {
    if (student == null) {
      return ReturnDTO.getReturnDTO(500, "未接收到参数值");
    }
    Student selectAloneStudent = studentMapper.selectAloneStudent(student);
    if (ObjectUtils.isEmpty(selectAloneStudent)) {
      return ReturnDTO.getReturnDTO(500, "接收失败");
    }
    return ReturnDTO.getReturnDTO(200, "接收成功", selectAloneStudent);
  }

  /**
   * 修改学生信息
   * @param student
   * @return
   */
  @Override
  public ReturnDTO updateStudent(Student student) {
    int rows = studentMapper.updateStudent(student);
    if (rows == 0){
      return ReturnDTO.getReturnDTO(500,"修改失败");
    }
    return ReturnDTO.getReturnDTO(200,"修改成功");
  }

  /**
   * 删除学生信息
   * @param student
   * @return
   */
  @Override
  public ReturnDTO deleteStudent(Student student) {
    int rows = studentMapper.deleteStudent(student);
    if (rows == 0){
      return  ReturnDTO.getReturnDTO(500,"删除失败");
    }

    return ReturnDTO.getReturnDTO(200,"删除成功");
  }
}
